using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class gpwatercs : MonoBehaviour
{
    public ComputeShader Shader;
    public Texture2D DEMMap;
    public Texture2D InitStateMap;
    RenderTexture waterVRT;
    int width, height;
    Renderer rend;
    RenderTexture outputTextureA;
    RenderTexture outputTextureB;
    int WaterVCS, WaterUpdateCS, WaterRainCS;
    bool isRuning = false;
    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();

        width = DEMMap.width;
        height = DEMMap.height;
       
        outputTextureA = new RenderTexture(width, height,0,RenderTextureFormat.ARGBFloat);
        outputTextureA.enableRandomWrite = true;
        outputTextureA.Create();
        outputTextureB = new RenderTexture(width, height, 0, RenderTextureFormat.ARGBFloat);
        outputTextureB.enableRandomWrite = true;
        outputTextureB.Create();

        waterVRT = new RenderTexture(width, height, 0, RenderTextureFormat.ARGBFloat);
        waterVRT.enableRandomWrite = true;
        waterVRT.Create();
        
        transform.localScale =new Vector3(width / height, 1, 1);

        WaterVCS = Shader.FindKernel("WaterVCS");
        WaterUpdateCS = Shader.FindKernel("WaterUpdateCS");
        WaterRainCS = Shader.FindKernel("WaterRainCS");
        
        Shader.SetInt("MaxSizeX", width - 1);
        Shader.SetInt("MaxSizeY", height - 1);
        Shader.SetFloat("viscosity", 0.001f);
        Shader.SetFloat("g", 9.8f);
        Shader.SetFloat("density", 1000.0f);
        //CAD
        Shader.SetFloat("resolution", 0.01f);
        Shader.SetFloat("detT", 0.0002f);
        Shader.SetFloat("constant_k", 0.01f);
        Shader.SetFloat("constant_n", 0.001f);
       

        Shader.SetTexture(WaterVCS, "gHeightMap", DEMMap);
        Shader.SetTexture(WaterUpdateCS, "gHeightMap", DEMMap);
        

        rend.material.SetTexture("_MainTex", outputTextureA);
        if (InitStateMap != null) 
            dispath(InitStateMap, outputTextureA);
        //for (int i = 0; i < 1; i++)
        //{
        //dispath(outputTextureA, outputTextureB);
        //dispath(outputTextureB, outputTextureA);
        //dispath(outputTextureA, outputTextureB);
        //dispath(outputTextureB, outputTextureA);    

        //}
        //initData();

    }
    void initData()
    {
        Unity.Collections.NativeArray<float> datas = InitStateMap.GetRawTextureData<float>();
        for (int i = 0; i < datas.Length; i++)
        {
            Debug.Log(datas[i]);
        }
        
    }
    void dispath(Texture textureA,Texture textureB)
    {
        //Shader.SetInt("MaxSizeX", width - 1);
        //Shader.SetInt("MaxSizeY", height - 1);
        //Shader.SetFloat("viscosity", 0.001f);
        //Shader.SetFloat("g", 9.8f);
        //Shader.SetFloat("resolution", 0.01f);
        //Shader.SetFloat("density", 1000.0f);
        //Shader.SetFloat("detT", 0.0002f);
        //Shader.SetFloat("constant_k", 0.01f);
        //Shader.SetFloat("constant_n", 0.001f);


        //Shader.SetTexture(WaterVCS, "gHeightMap", DEMMap);
        //Shader.SetTexture(WaterUpdateCS, "gHeightMap", DEMMap);
        //Shader.SetTexture(WaterVCS, "gHeightMap", DEMMap);
        Shader.SetTexture(WaterVCS, "gWaterMapIn", textureA);
        Shader.SetTexture(WaterVCS, "gWawterVOut", waterVRT);
        Shader.Dispatch(WaterVCS, width / 8, height / 8, 1);

        //Shader.SetTexture(WaterUpdateCS, "gHeightMap", DEMMap);
        Shader.SetTexture(WaterUpdateCS, "gWaterMapIn", textureA);
        Shader.SetTexture(WaterUpdateCS, "gWawterVIn", waterVRT);
        Shader.SetTexture(WaterUpdateCS, "gWaterMapOut", textureB);
        Shader.Dispatch(WaterUpdateCS, width / 8, height / 8, 1);

        
    }
    void RainCSDispath()
    {
        Shader.SetTexture(WaterRainCS, "gWaterMapIn", outputTextureB);
        Shader.SetTexture(WaterRainCS, "gWaterMapOut", outputTextureA);
        Shader.Dispatch(WaterRainCS, width / 8, height / 8, 1);
    }
    public void OnKValueChanged(float value)
    {
        Debug.Log(value);
        //Shader.SetFloat("constant_k", value);
    }
    // Update is called once per frame
    void Update()
    {
        //if (flag)
        //{
        //    flag = !flag;
        //    Shader.SetTexture(UpdatewaterHandle, "Result", outputTextureA);
        //    Shader.SetTexture(UpdatewaterHandle, "WaterMapIn", InitStateMap);
        //}
        //else
        //{

        //}
        
        if (Input.GetKeyDown("p"))
        {
            Debug.Log("isruning");
            isRuning = !isRuning;
        }
        if(Input.GetKey("r"))
        {
            RainCSDispath();
        }
        if (isRuning)
        {
            dispath(outputTextureA, outputTextureB);
            dispath(outputTextureB, outputTextureA);
        }
    }
}
